﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using AjaxControlToolkit;
using DataAccess;
using SubSonic;


/// <summary>
/// Summary description for DatabaseLocations
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class DatabaseLocations : System.Web.Services.WebService {

    public DatabaseLocations () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    
    [WebMethod]
    public CascadingDropDownNameValue[] GetCity(string knownCategoryValues,string category) 
    {
        DataAccess.TblCityCollection tbl = new TblCityCollection();
        tbl.LoadAndCloseReader( TblCity.FetchAll(OrderBy.Asc("Name")) );
        List<CascadingDropDownNameValue> ret = new List< CascadingDropDownNameValue >();
        ret.Sort();
        foreach ( var VARIABLE in tbl.GetList() )
        {
            ret.Add(new CascadingDropDownNameValue(VARIABLE.Name, VARIABLE.Id.ToString()  ));
        }
        return ret.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetBakhsh(string knownCategoryValues, string category)
    {
        DataAccess.TblBakhshCollection tbl = new TblBakhshCollection();
        string[] key = knownCategoryValues.Split(':', ';');
        tbl.LoadAndCloseReader(TblBakhsh.FetchByParameter(TblBakhsh.Columns.CityId, Comparison.Equals, new Guid(key[1]), OrderBy.Asc("Name")));
        List<CascadingDropDownNameValue> ret = new List<CascadingDropDownNameValue>();
        foreach (var variable in tbl.GetList())
        {
            ret.Add(new CascadingDropDownNameValue(variable.Name, variable.Id.ToString()));
        }
        return ret.ToArray();
    }


    [WebMethod]
    public CascadingDropDownNameValue[] GetDehestan(string knownCategoryValues, string category)
    {
        TblDehestanCollection tbl = new TblDehestanCollection();
        
        string[] key = knownCategoryValues.Split(':', ';');

        tbl.LoadAndCloseReader(TblDehestan.FetchByParameter(TblDehestan.Columns.BakhshId, Comparison.Equals, new Guid(key[3]), OrderBy.Asc("Name")));

        List<CascadingDropDownNameValue> ret = new List<CascadingDropDownNameValue>();
        foreach (var variable in tbl.GetList())
        {
            ret.Add(new CascadingDropDownNameValue(variable.Name, variable.Id.ToString()));
        }
        return ret.ToArray();
    }


        [WebMethod]
    public CascadingDropDownNameValue[] GetVillage(string knownCategoryValues, string category)
    {
        TblVillageCollection tbl = new TblVillageCollection();

                string[] key = knownCategoryValues.Split(':', ';');

                tbl.LoadAndCloseReader(TblVillage.FetchByParameter(TblVillage.Columns.DehstanId, Comparison.Equals, new Guid(key[5]), OrderBy.Asc("Name")));

        List<CascadingDropDownNameValue> ret = new List<CascadingDropDownNameValue>();
        foreach (var VARIABLE in tbl.GetList())
        {
            ret.Add(new CascadingDropDownNameValue(VARIABLE.Name, VARIABLE.Id.ToString()));
        }
        return ret.ToArray();
    }
}
